//
//  ContainerViewController.m
//  SliderViewDemo
//
//  Created by RaoQiangHua on 15/8/29.
//  Copyright (c) 2015年 RaoQiangHua. All rights reserved.
//

#import "ContainerViewController.h"

@interface ContainerViewController ()

@end

@implementation ContainerViewController

/*
 * 思路：容器控制器中包含了主控制器和侧边控制器的view，然后
 * 所有的手势都是在容器控制器中实现，实现的过程是对两个view
 * 的位置和大小进行调整。
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    _mainViewController = (MainViewController*)[storyboard instantiateViewControllerWithIdentifier:@"MainViewController"];
    _mainViewController.fatherViewController = self;
    
    _slideViewController = (SlideViewController*)[storyboard instantiateViewControllerWithIdentifier:@"SlideViewController"];
    
    /*把主控制器的view加到容器控制器中*/
    [self.view addSubview:_mainViewController.view];
    [_mainViewController.view setTag:1];
    [_mainViewController.view setFrame:self.view.bounds];
    
    /*把侧边控制器的view加到容易控制器中*/
    [self.view addSubview:_slideViewController.view];
    [_slideViewController.view setTag:2];
    [_slideViewController.view setFrame:self.view.bounds];
    
    /*初始化时，把主控制器的view显示到最前边*/
    [self.view bringSubviewToFront:_mainViewController.view];
    
    /*增加滑动手势*/
    UISwipeGestureRecognizer* swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                                                            action:@selector(swipeGesture:)];
    [swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [_mainViewController.view addGestureRecognizer:swipeGestureRight];
    
}

-(void) swipeGesture:(UISwipeGestureRecognizer*)swipeGestureRecognizer{
    CALayer* layer = [_mainViewController.view layer];
    layer.shadowColor = [UIColor blackColor].CGColor;
    layer.shadowOffset = CGSizeMake(1, 1);
    layer.shadowOpacity = 1;
    layer.shadowRadius = 20.0;
    if(swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight){
        [self showSlider];
    }
    
    if(swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionLeft){
        [self hideSlider];
    }
}

/*显示侧边栏，单独写出一个函数，用于主控制器调用*/
-(void)showSlider{
    [_slideViewController.view setHidden:NO];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    if(_mainViewController.view.frame.origin.x == self.view.frame.origin.x ||
       _mainViewController.view.frame.origin.x == -200){
        [_mainViewController.view setFrame:CGRectMake(_mainViewController.view.frame.origin.x + 200,
                                                      _mainViewController.view.frame.origin.y,
                                                      _mainViewController.view.frame.size.width,
                                                      _mainViewController.view.frame.size.height)];
    }
    
    [UIView commitAnimations];
}

-(void)hideSlider{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    if(_mainViewController.view.frame.origin.x == 200){
        [_mainViewController.view setFrame:CGRectMake(_mainViewController.view.frame.origin.x-200,
                                                      _mainViewController.view.frame.origin.y,
                                                      _mainViewController.view.frame.size.width,
                                                      _mainViewController.view.frame.size.height)];
    }
    
    [UIView commitAnimations];
    [_slideViewController.view setHidden:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
